package com.swufe.lianxi;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.nio.charset.MalformedInputException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class RatelistActivity2 extends ListActivity implements Runnable,AdapterView.OnItemClickListener {
    private static final String TAG = "ratelist2";
    Handler handler;
    ArrayList<HashMap<String, String>> listItems;
    private SimpleAdapter listItemAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_ratelist2);
        initListView();
        this.setListAdapter(listItemAdapter);

        Thread t = new Thread(this);
        t.start();
        handler = new Handler(Looper.myLooper()) {
            @Override
            //收到数据
            public void handleMessage(Message msg) {
                if (msg.what == 5) {
                    listItems = (ArrayList<HashMap<String, String>>) msg.obj;
                    listItemAdapter = new SimpleAdapter(RatelistActivity2.this, listItems, R.layout.list_item, new String[]{"ItemTitle", "ItemDetail"}, new int[]{R.id.itemTitle, R.id.itemDetail});
                    //MyAdapter myAdapter = new MyAdapter(RatelistActivity2.this, R.layout.list_item, list);
                    //listView.setAdapter(listItemAdapter);
                    setListAdapter(listItemAdapter);
                }
                super.handleMessage(msg);
            }
        };
        getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Log.i(TAG, "onItemClick: parent=" + adapterView);
                Log.i(TAG, "onItemClick: View=" + view);
                Log.i(TAG, "onItemClick: position=" + i);
                Log.i(TAG, "onItemClick: id=" + l);
                HashMap<String, String> map = (HashMap<String, String>) getListView().getItemAtPosition(i);
                String titleStr = map.get("ItemTitle");
                String detailStr = map.get("ItemDetail");
                Log.i(TAG, "onItemClick: titleStr=" + titleStr);
                Log.i(TAG, "onItemClick: detailStr=" + detailStr);

                TextView title=(TextView)view.findViewById(R.id.itemTitle);
                TextView detail=(TextView)view.findViewById(R.id.itemDetail);
                String title2=String.valueOf(title.getText());
                String detail2=String.valueOf(detail.getText());
                Log.i(TAG, "onItemClick: title2="+title2);
                Log.i(TAG, "onItemClick: detail2="+detail2);
                //打开新的页面，传递参数
                Intent intent=new Intent(RatelistActivity2.this,RateCalcActivity.class);
                intent.putExtra("title",titleStr);
                intent.putExtra("detail",detailStr);
                intent.putExtra("rate",Float.parseFloat(detailStr));
                startActivity(intent);
            }
        });
    }



    private void initListView() {
        //准备测试数据
        ArrayList<HashMap<String, String>> listItems = new ArrayList<HashMap<String, String>>();
        for (int i = 0; i < 100; i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("ItemTitle", "Rate:" + i);
            map.put("ItemDetail", "detail" + i);
            listItems.add(map);
        }
        //生成适配器的item和动态数组对应的元素
         listItemAdapter = new SimpleAdapter(this,
                listItems,
                R.layout.list_item,
                new String[]{"ItemTitle", "ItemDetail"},
                new int[]{R.id.itemTitle, R.id.itemDetail});

        //ListView listView = findViewById(R.id.ratelist);
        //listView.setListAdapter(listItemAdapter);
        //ListView.setAdapter(adapter);
        //ListView.setEmptyView(findViewById(R.id.nodata));
    }

    public void run() {
        //获取网络数据，放入list带回到主线程中
        List<HashMap<String, String>> retlist = new ArrayList<HashMap<String, String>>();
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Bundle bundle = new Bundle();
        Document doc = null;
        try {
            String url = "https://www.boc.cn/sourcedb/whpj/ ";
            doc = Jsoup.connect(url).get();
            Log.i(TAG, "run: " + doc.title());
            Elements tables = doc.getElementsByTag("table");
            Element table1 = tables.get(1);
            Log.i(TAG, "run: table1=" + table1);
            Elements tds = table1.getElementsByTag("td");
            for (int i = 0; i < tds.size(); i += 8) {
                Element td1 = tds.get(i);
                Element td2 = tds.get(i + 5);
                String str1 = td1.text();
                String val = td2.text();
                Log.i(TAG, "run: " + str1 + "==>" + val);
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("ItemTitle", str1);
                map.put("ItemDetail", val);
                retlist.add(map);
            }
        } catch (MalformedInputException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Message msg = handler.obtainMessage(5);
        msg.obj = retlist;
        handler.sendMessage(msg);
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.i(TAG, "onItemClick: parent=" + parent);
        Log.i(TAG, "onItemClick: View=" + view);
        Log.i(TAG, "onItemClick: position=" + position);
        Log.i(TAG, "onItemClick: id=" + id);
        HashMap<String, String> map = (HashMap<String, String>) getListView().getItemAtPosition(position);
        String titleStr = map.get("ItemTitle");
        String detailStr = map.get("ItemDetail");
        Log.i(TAG, "onItemClick: titleStr=" + titleStr);
        Log.i(TAG, "onItemClick: detailStr=" + detailStr);

        TextView title=(TextView)view.findViewById(R.id.itemTitle);
        TextView detail=(TextView)view.findViewById(R.id.itemDetail);
        String title2=String.valueOf(title.getText());
        String detail2=String.valueOf(detail.getText());
        Log.i(TAG, "onItemClick: title2="+title2);
        Log.i(TAG, "onItemClick: detail2="+detail2);


    }
}